<template>
    <div class="bg-white w-full select-none rounded-lg" v-if="isLoad">
        <ypform :formData="formData" :group="mod.colgroup" :mod="mod" @save="save"></ypform>
    </div>
</template>
<script setup>
import { postData,getData, alter } from '@/common/base'
import { ref, reactive, onMounted } from 'vue'
const props = defineProps({
    mod: {
        type: Object
    }
})
const isLoad=ref(false)
const formData = reactive({})

const save = async (postParams) => {
    const ajaxData={modname:props.mod.name,data:postParams}
    const res = await postData("model/save", ajaxData)
    if (res.code == 1) {
        alter({ type: 'alter-success', text: res.msg })
    }
}

const getResult = async () => {
    const resp= await getData("model",{modname:props.mod.name})
    if(resp.code==1){
        let res=resp.data[0]
        formData['id']=res.id
        props.mod.modcolumn.forEach((v) => {
            if (v.formitem != 'none') {
                formData[v.name] = res[v.name]
            }
        })
    }
}

onMounted(async() => {
    await getResult()
    isLoad.value=true
})
</script>
